/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectEmployeesDialog.java
 *
 * Created on 11-Oct-2010, 21:18:03
 */

package TimeRecording.forms;

import TimeRecording.forms.entities.SelectableEmployee;
import TimeRecording.tables.Employee;
import java.awt.Window;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.persistence.EntityManager;
import org.jdesktop.observablecollections.ObservableCollections;

/**
 *
 * @author david
 */
public class SelectEmployeesDialog extends javax.swing.JDialog {

    private boolean okSelected;

    /** Creates new form SelectEmployeesDialog */
    @SuppressWarnings("unchecked")
    public SelectEmployeesDialog(Window parent,
            EntityManager entityManager, boolean modal) {
        
        super(parent, modal ?
            ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);

        if (Beans.isDesignTime()) {
            employeeList = Collections.emptyList();
            
        } else {
            List<Employee> employees = Employee.getActiveEmployees(entityManager);
            employeeList = new ArrayList<SelectableEmployee>();

            for (Employee employee : employees) {
                employeeList.add(new SelectableEmployee(employee, false));
            }
        }

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        employeeList = ObservableCollections.observableList(employeeList);
        emplyeePane = new javax.swing.JScrollPane();
        employeeTable = new javax.swing.JTable();
        buttonPanel = new javax.swing.JPanel();
        selectAllButton = new javax.swing.JButton();
        selectNoneButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(SelectEmployeesDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        emplyeePane.setName("emplyeePane"); // NOI18N

        employeeTable.setAutoCreateRowSorter(true);
        employeeTable.setColumnSelectionAllowed(true);
        employeeTable.setName("employeeTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, employeeList, employeeTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${employee}"));
        columnBinding.setColumnName("Employee");
        columnBinding.setColumnClass(TimeRecording.tables.Employee.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${selected}"));
        columnBinding.setColumnName("Selected");
        columnBinding.setColumnClass(Boolean.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        emplyeePane.setViewportView(employeeTable);
        employeeTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        employeeTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("employeeTable.columnModel.title0")); // NOI18N
        employeeTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("employeeTable.columnModel.title1")); // NOI18N

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(emplyeePane, gridBagConstraints);

        buttonPanel.setName("buttonPanel"); // NOI18N

        selectAllButton.setText(resourceMap.getString("selectAllButton.text")); // NOI18N
        selectAllButton.setName("selectAllButton"); // NOI18N
        selectAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectAllButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(selectAllButton);

        selectNoneButton.setText(resourceMap.getString("selectNoneButton.text")); // NOI18N
        selectNoneButton.setName("selectNoneButton"); // NOI18N
        selectNoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectNoneButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(selectNoneButton);

        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(cancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        getContentPane().add(buttonPanel, gridBagConstraints);

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-399)/2, (screenSize.height-338)/2, 399, 338);
    }// </editor-fold>//GEN-END:initComponents

    private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
        for (SelectableEmployee employee : employeeList) {
            employee.setSelected(true);
        }
    }//GEN-LAST:event_selectAllButtonActionPerformed

    private void selectNoneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectNoneButtonActionPerformed
        for (SelectableEmployee employee : employeeList) {
            employee.setSelected(false);
        }
    }//GEN-LAST:event_selectNoneButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        okSelected = true;
        setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        okSelected = false;
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private java.util.List<SelectableEmployee> employeeList;
    private javax.swing.JTable employeeTable;
    private javax.swing.JScrollPane emplyeePane;
    private javax.swing.JButton okButton;
    private javax.swing.JButton selectAllButton;
    private javax.swing.JButton selectNoneButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public boolean getOkSelected() {
        return okSelected;
    }

    public ArrayList<Employee> getSelectedEmployees() {

        ArrayList<Employee> selected = new ArrayList<Employee>();

        if (okSelected) {
            for (SelectableEmployee employee : employeeList) {
                if (employee.getSelected()) {
                    selected.add(employee.getEmployee());
                }
            }
        }
        
        return selected;
    }

    void select(Employee selectedEmployee) {
        for (SelectableEmployee employee : employeeList) {
            if (employee.getEmployee().getEmployeeId().intValue() ==
                    selectedEmployee.getEmployeeId().intValue()) {
                employee.setSelected(true);
            }
        }
    }
}